/**
 * Created by xiaopingfeng on 10/3/17.
 */

var fs = require('fs')
var readline = require('readline');
var net = require('net');

class MockSender {

  constructor(file) {
    this.data_file = file
    console.log('data', this.data_file)
  }

  start_send() {
    var client = new net.Socket()
    client.connect(8899, '127.0.0.1', function () {
      var lines = []
      var myInterface = readline.createInterface({
        input: fs.createReadStream('test/raw/192.168.0.4_56155.raw')
      });
      myInterface.on('line', function (line) {
        var data = Buffer.from(line, 'base64')
        lines.push(data)
      });
      myInterface.on('close', function () {
        var i = 0
        var task = setInterval(function () {
          i++
          // TODO send to server
          if (lines[i]) {
            client.write(lines[i]);
          }

          if (i == lines.length) {
            console.log('complete')
            clearInterval(task)
          }
        }, 50)
      })
    })
  }
}

var sender = new MockSender()
sender.start_send()

